package controller;

import model.Master;
import model.User;
import model.game_list;
import model.play_list;
import org.apache.tools.ant.types.selectors.SelectSelector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import service.MasterService;
import service.UserService;
import util.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;


@Controller
public class MasterController {


    @Autowired

    private MasterService masterService;
    private UserService userService;



    @RequestMapping("master_login")

    public String toLogin() {

        return "master_login";

    }



    @RequestMapping("check_master_login")

    public String check_master_login(@RequestParam(defaultValue = "1") int currentPage,
                                     HttpServletRequest request,
                                     HttpServletResponse response, Model model)throws Exception {


        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("username", "");
        map.put("gender", "");
        System.out.println(map);

        Page<User> page = masterService.findByPage(currentPage, map);
        model.addAttribute("pageData", page);//设置一个属性
        model.addAttribute("params", map);




        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
//        String username = request.getParameter("username");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("username=" + username);

        System.out.println("password=" + password);


        Master master=masterService.check_master_login(username, password);
        model.addAttribute("Master_name",username);
        System.out.println(master);

        if(master!=null){
            return "user_list";
        }
        else
        return "redirect:master_login";

    }





@RequestMapping("user_list")
public String getPage(@RequestParam(defaultValue = "1") int currentPage, Model model,

                      HttpServletRequest request,String code, String username, String gender) {

    HashMap<String, Object> map = new HashMap<String, Object>();

    map.put("username", username);
    map.put("gender", gender);

    System.out.println(map);

    Page<User> page = masterService.findByPage(currentPage, map);

    model.addAttribute("pageData", page);//设置一个属性
    model.addAttribute("params", map);
   // model.addAttribute("username",username);
    return "user_list";

}










    @RequestMapping("to_apply_list")

    public String apply_list(@RequestParam(defaultValue = "1") int currentPage,
                             Model model,String game_type, String game_name,String player,HttpServletRequest request,String Master_name) {

        HashMap<String, Object> map = new HashMap<String, Object>();


//        map.put("id", id);
        map.put("game_type", game_type);
        map.put("game_name", game_name);
        map.put("player", player);

        model.addAttribute("Master_name",Master_name);


        System.out.println(map);

        Page<game_list> page = masterService.findByPage_apply(currentPage, map);




        List<game_list> game_list_m=null;
        game_list_m = masterService.get_game_list_m();
        System.out.println(game_list_m);
        model.addAttribute("game_list_m",game_list_m);
        //request.setAttribute("game_list_m",game_list_m);




        model.addAttribute("pageData", page);//设置一个属性
        model.addAttribute("params", map);
        // model.addAttribute("username",username);
        return "apply_list";



    }




   @RequestMapping(value = "approve_master/{id}",method = RequestMethod.GET)
    public String master_approve(@PathVariable int id,Model model){

        game_list game_list=masterService.find_game_list_ById(id);

     //  model.addAttribute("data",game_list);
       masterService.update_play_state(game_list);
        return "apply_list";

    }




    @RequestMapping(value = "edit_master/{id}",method = RequestMethod.GET)
    public String update_game_list(@PathVariable int id,Model model){

        game_list game_list=masterService.find_game_list_ById(id);

          model.addAttribute("data",game_list);
        return "update_game_list";

    }





    @RequestMapping(value = "edit_master",method = RequestMethod.POST)
    public String update_game_list(game_list game_list){

        masterService.update_game_list(game_list);

        return "apply_list";

    }


    @RequestMapping(value="delete_master/{id}",method = RequestMethod.GET)

    public String delete(@PathVariable int id) {
                System.out.println("删除学生id==" + id);
                masterService.delete(id);
        return "apply_list";

    }




}
